package cn.com.frame.util;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 数据类型转换工具类
 */
public class Cast {
    public static String toString(Object obj, String defaultvalue) {
        if (obj == null)
            return defaultvalue;
        else {
            if(obj instanceof String)
                return (String) obj;
            else{
                return obj.toString();
            }

        }
    }
    public static int toInt(Object obj, int defaultValue){
        if(obj == null) return defaultValue;
        try{
            if(obj instanceof Integer) return (Integer)obj;
            else if(obj instanceof BigDecimal) {
                BigDecimal d = (BigDecimal)obj;
                return d.intValue();
            }else if(obj instanceof Double) {
                Double d = (Double)obj+0.5;
                return d.intValue();
            }else{
                int n = Integer.parseInt(obj.toString());
                return n;
            }
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static short toShort(Object obj, short defaultValue){
        if(obj == null) return defaultValue;
        try{
            if(obj instanceof Integer) return (Short)obj;
            else if(obj instanceof BigDecimal) {
                BigDecimal d = (BigDecimal)obj;
                return d.shortValue();
            }else if(obj instanceof Double) {
                Double d = (Double)obj+0.5;
                return d.shortValue();
            }else{
                short n = Short.parseShort(obj.toString());
                return n;
            }
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static Long toLong(Object obj, Long defaultValue){
        if(obj == null) return defaultValue;
        try{
            if(obj instanceof Long) return (Long)obj;
            else if(obj instanceof BigDecimal) {
                BigDecimal d = (BigDecimal)obj;
                return d.longValue();
            }else if(obj instanceof Double) {
                Long d = Math.round((Double)obj);
                return d;
            }else{
                Long n = Long.parseLong(obj.toString());
                return n;
            }
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static double toDouble(Object obj, double defaultValue){
        if(obj == null) return defaultValue;
        try{
            if(obj instanceof Double) {
                Double d = (Double)obj;
                return d;
            }
            else if(obj instanceof BigDecimal) {
                BigDecimal d = (BigDecimal)obj;
                return d.doubleValue();
            }else if(obj instanceof Integer){
                return (Integer)obj;
            }else {
                double d = Double.parseDouble(obj.toString());
                return d;
            }
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static byte toByte(Object obj, Byte defaultValue){
        if(obj == null) return defaultValue;
        try{
            int n = toInt(obj, defaultValue);
            return (byte)n;
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static Date toDate(Object obj, Date defaultValue){
        if(obj == null) return defaultValue;
        try{
            java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp)obj;
            //Date date = (Date)sqlTimestamp;
            return sqlTimestamp;
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static java.sql.Date toDate(Object obj, java.sql.Date defaultValue){
        if(obj == null) return defaultValue;
        try{
            java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp)obj;
            java.sql.Date date = new java.sql.Date(sqlTimestamp.getTime());
            return date;
        }catch(Exception e){
            return defaultValue;
        }
    }
    public static java.sql.Timestamp toDate(Object obj, java.sql.Timestamp defaultValue){
        if(obj == null) return defaultValue;
        try{
            java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp)obj;
            return sqlTimestamp;
        }catch(Exception e){
            return defaultValue;
        }
    }
}
